package org.rich.charlesmurphy.ui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.ddevil.data.set.BeanDataSet;
import org.ddevil.data.set.DataSet;
import org.ddevil.data.util.DataSetUtils;
import org.rich.charlesmurphy.core.CMIds;
import org.rich.charlesmurphy.model.CharlieMurphyModel;
import org.rich.charlesmurphy.model.PageItem;
import org.rich.charlesmurphy.model.RequestGroup;
import org.windowkit.toolkit.frame.WindowLauncher;
import org.windowkit.toolkit.util.LayoutUtils;
import org.windowkit.toolkit.widget.list.ListPane;
import org.windowkit.toolkit.widget.table.RecordTablePane;


/**
 *
 * @author Rich O'Connell
 */
public class ViewByPage extends AbstractCharliePanel {
	private static final long serialVersionUID = -5897450763648962841L;

	private ListPane<PageItem> lstPages;
	private ViewRequestGroups pnlTablePane;
	private GroupTableTopPanel pnlPageInfo;

	public ViewByPage(CharlieMurphyModel model, WindowLauncher launcher) {
		super(model.getSessionName() + " [Grouped by Page]");
		setLauncher(launcher);
		setModel(model);
	}

	public void setModel(CharlieMurphyModel model) {
		pnlTablePane.setModel(model.getGroupedData());
		setTitle(getTitle() + " [Total: " + model.getGroupedData().size() + "]");
		DataSet<PageItem> pages = new BeanDataSet<PageItem>();
		pages.importRecord(new PageItem("", "").setPageTitle("ALL"));
		pages.importRecords(model.getPageData(CMIds.MAX_REQUESTS));
		lstPages.setDataSet(pages);
		lstPages.setBorder(BorderFactory.createTitledBorder("Pages [Total:" + (pages.size() - 1)  + "]"));
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void initComponents() {
		lstPages = new ListPane<PageItem>();
		lstPages.setBorder(BorderFactory.createTitledBorder("Pages"));
		lstPages.setMainAttribute(CMIds.PAGE_TITLE);
		pnlPageInfo = new GroupTableTopPanel();
		pnlTablePane = new ViewRequestGroups("");
	}

	@Override
	public void setLauncher(WindowLauncher launcher) {
		super.setLauncher(launcher);
		pnlTablePane.setLauncher(launcher);
	}

	@Override
	protected JComponent createContents() {
		JPanel cp = new JPanel(new GridBagLayout());
		GridBagConstraints gbc = LayoutUtils.createConstraintsFillBoth();
		gbc.insets = new Insets(3, 3, 3, 3);
		gbc.gridx = 0;
		gbc.gridy = 0;

		cp.add(lstPages, gbc);

		JSplitPane splitter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
				new JScrollPane(cp), createTablePane());
		splitter.setDividerLocation(280);
		splitter.setDividerSize(4);

		return splitter;
	}

	protected JComponent createTablePane() {
		JPanel ret = new JPanel(new BorderLayout());
		ret.add(pnlPageInfo, BorderLayout.NORTH);
		ret.add(pnlTablePane, BorderLayout.CENTER);
		return ret;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void addListeners() {
		lstPages.addListSelectionListener(new ListSelectionListener(){
			public void valueChanged(ListSelectionEvent e) {
				if(!e.getValueIsAdjusting()) {
					handlePageSelected();
				}
			}
		});
	}

	protected void handlePageSelected() {
		if (lstPages.getSelectedIndex() == 0) {
			getTable().setFilter(CMIds.PATH, "");
			getTable().setFilter(CMIds.METHOD, "");
			getTable().setColumnVisible(CMIds.METHOD, true);
			getTable().setColumnVisible(CMIds.PATH, true);
			pnlPageInfo.clear();
		}
		else {
			PageItem pi = lstPages.getSelectedRecord();
			getTable().setFilter(CMIds.PATH, pi.getPageName());
			getTable().setFilter(CMIds.METHOD, pi.getMethod());
			getTable().setColumnVisible(CMIds.METHOD, false);
			getTable().setColumnVisible(CMIds.PATH, false);
			pnlPageInfo.setModel(pi);
		}
	}

	protected RecordTablePane getTable() {
		return pnlTablePane.getTable();
	}


	class GroupTableTopPanel extends JPanel {
		private static final long serialVersionUID = 2818901629526257734L;
		private JLabel url;
		private JLabel maxRequests;
		public GroupTableTopPanel() {
			super(new BorderLayout());
			url = new JLabel();
			maxRequests = new JLabel();
			add(url);
			add(maxRequests);
		}
		public void clear() {
			url.setText("");
		}
		public void setModel(PageItem item) {
			DataSet<RequestGroup> samples = item.getSamples();
			double reqMax = DataSetUtils.max(samples, CMIds.REQUESTS);

			url.setText(item.getMethod() + ": " + item.getPageName());
			maxRequests.setText("Max Requets: " + reqMax);
		}
	}

}
